package com.exam.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MapListToEntityList {

  public static <T> List<T> mapListToEntityList(List<Map<String, Object>> mapList, Class<T> entityClass) throws InstantiationException, IllegalAccessException {
    List<T> entityList = new ArrayList<>();
    for (Map<String, Object> map : mapList) {
      T entity = entityClass.newInstance();
      for (Field field : entityClass.getDeclaredFields()) {
        field.setAccessible(true); // 设置字段可访问
        field.set(entity, map.get(field.getName())); // 设置字段值
      }
      entityList.add(entity);
    }
    return entityList;
  }
}
